<template>
  <div class=""></div>
</template>

<script>
export default {
  props: {},
  components: {
    // components: (reslove) => {
    // require(['', reslove]);
    // }
  },
  data() {
    return {};
  },
  created() {
    this.initDataFun(); //初始化数据
  },
  mounted() {},
  beforeDestroy() {},
  watch: {},
  computed: {},
  methods: {
    //初始化数据
    initDataFun() {
      this.testArrFunc();
    },
    // #region 数组差集、并集、交集 [注意：主数组不一样，可能结果的信息就有差别]
    testArrFunc() {
      let arr1 = [
        { id: 1, name: 'Bob' },
        { id: 2, name: 'Jmu' },
        { id: 3, name: 'Timi' },
      ];
      let arr2 = [
        { id: 1, name: 'Bob' },
        { id: 2, name: 'Matis' },
        { id: 4, name: 'Com' },
        { id: 5, name: 'Coco' },
      ];
      subtractionFunc();
      UnionFunc();
      intersectionFunc();

      // 差集 : id
      function subtractionFunc() {
        // 第二个数组 相比于 第一个数组的差集
        let result = arr2.filter((v) => {
          return arr1.every((i) => i.id != v.id);
        });
        console.log('差集', result); // [ {id: 4, name: "Com"}, {id: 5, name: "Coco"}]

        // 第一个数组 相比于 第二个数组的差集
        let result1 = arr1.filter((v) => {
          return arr2.every((i) => i.id != v.id);
        });
        console.log('差集', result1); // [ {id: 3, name: "Timi"}]
      }

      // 并集 : id
      function UnionFunc() {
        let arr3 = arr1.concat(arr2);
        let obj = [],
          result2 = [];
        result2 = arr3.reduce((prev, cur) => {
          obj[cur.id] ? '' : (obj[cur.id] = true && prev.push(cur));
          return prev;
        }, []);
        console.log('并集', result2); // [ {id: 1, name: "Bob"},{id: 2, name: "Jmu"},{id: 3, name: "Timi"},{id: 4, name: "Com"},{id: 5, name: "Coco"}]
      }

      // 交集 : id
      function intersectionFunc() {
        // 取第一个数组的id，在第二个数组中比较
        let arr4 = arr1.map((item) => item.id);
        let result3 = arr2.filter((v) => {
          return new Set(arr4).has(v.id);
        });
        console.log(result3); // [{id: 1, name: "Bob"}, {id: 2, name: "Matis"}]

        // 取第二个数组的id，在第一个数组中比较
        let arr5 = arr2.map((item) => item.id);
        let result4 = arr1.filter((v) => {
          return new Set(arr5).has(v.id);
        });
        console.log('交集', result4); // [{id: 1, name: "Bob"},{id: 2, name: "Jmu"}]
      }
    },
    // #endregion
  },
};
</script>

<style lang="less"></style>

<style lang="less" scoped></style>
